﻿namespace NTLib.Core.UTest.Configuration.Attributes
{
    using Core.Configuration;
    using Core.Configuration.Attributes;
    using Core.Configuration.Values;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Serialization;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;

    [TestClass]
    public class EmbeddedConfigurationAttrUTest
    {
        #region Nested

        public class ConfigTest
        {
            #region Properties

            public string Test { get; set; }

            public int Value { get; set; }

            #endregion
        }

        [EmbeddedConfiguration(typeof(ConfigTest), "NTLib.Core.UTest.Configuration.Attributes.EmbeddedJsonFile.json", typeof(ObjectContainerValue<ConfigTest>), typeof(DataContractJsonSerializerSlim), ThrowIfFileNotFound = true)]
        public class ToConfig : BaseConfigurable
        {
            #region Properties

            public ConfigurationCollection Collection { get; set; }

            #endregion

            protected override void OnLoadConfig(ConfigurationCollection configs)
            {
                this.Collection = configs;
            }

            protected override void OnResetConfig()
            {
            }
        }

        #endregion

        #region Methods

        [TestCategory(ConfigManagerUTest.CONFIG_CATEGORIE)]
        [TestMethod]
        public void Config_EmbeddedSimpleObj()
        {
            ToConfig config = new ToConfig();

            ConfigManager.Configure(config);

            Assert.IsNotNull(config.Collection);
            Assert.AreEqual(1, config.Collection.Count);
            Assert.AreEqual(1, config.Collection.First().Value.Count);

            var configTest = config.Collection.First().Value.FirstOrDefault();
            Assert.IsInstanceOfType(configTest, typeof(ObjectContainerValue<ConfigTest>));
            var result = configTest as ObjectContainerValue<ConfigTest>;
            Assert.AreEqual("toto", result.Data.Test);
            Assert.AreEqual(42, result.Data.Value);
            Assert.AreEqual("default", result.Key);

        }

        #endregion
    }
}
